﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using SOM.ConcreteDAL;
using SOM.Model;
using SOM.AbstractDALFactory;
using SOM.ConcreteDALFactory;

namespace SOM.Security
{
    public static class SOMSecurity
    {
        private static string _key = "fabionovaissomproject";
        
        /// <summary>
        /// Verifica credenciais do usuário
        /// </summary>
        /// <param name="nickname"></param>
        /// <param name="senha"></param>
        /// <returns></returns>
        public static bool Autenticar(string nickname, string senha)
        {
            SOMAbstractDALFactory factory = new SOMConcreteDALFactory();
            UsuarioDAL usuarioDAL = factory.CreateUsuarioDAL();

            Usuario usuario = usuarioDAL.FindByNickname(nickname);

            if (usuario == null)
            {
                return false;
            }

            if (usuario.usr_senha.Equals(SOMSecurity.MD5Criptografar(senha)))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Criptografa uma string utilizando o algoritmo MD5
        /// </summary>
        /// <param name="senha"></param>
        /// <returns></returns>
        public static string MD5Criptografar(string senha)
        {
            string resultado = "";

            byte[] mensagem = System.Text.Encoding.Default.GetBytes(senha);

            MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();

            byte[] hashCode = md5Provider.ComputeHash(mensagem);

            for (int i = 0; i < hashCode.Length; i++)
            {
                resultado += (char)(hashCode[i]);
            }

            return resultado;
        }


        /// <summary>
        /// Método que codifica string
        /// </summary>
        /// <param name="strTexto">string</param>
        /// <returns>string codificada</returns>
        public static string Codificar(string strTexto)
        {
            //DESCryptoServiceProvider oDes = new DESCryptoServiceProvider();
            //byte[] IV = { 121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62 };
            //byte[] bytValor = System.Text.Encoding.ASCII.GetBytes(strTexto.ToCharArray());
            //byte[] bytKey = System.Text.Encoding.ASCII.GetBytes(_key.Substring(0, 8).ToCharArray());

            //MemoryStream oMemory = new MemoryStream();
            //CryptoStream oCStream = new CryptoStream(oMemory, oDes.CreateEncryptor(bytKey, IV), CryptoStreamMode.Write);
            //oCStream.Write(bytValor, 0, bytValor.Length);
            //oCStream.FlushFinalBlock();

            //return Convert.ToBase64String(oMemory.ToArray());

            return strTexto;
        }

        /// <summary>
        /// Método que decodifica string
        /// </summary>
        /// <param name="strTexto">string codificada</param>
        /// <returns>string decodificada</returns>
        public static string Decodificar(string strTexto)
        {
            //DESCryptoServiceProvider oDes = new DESCryptoServiceProvider();
            //byte[] IV = { 121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62 };
            //byte[] bytValor = Convert.FromBase64String(strTexto);
            //byte[] bytKey = Encoding.ASCII.GetBytes(_key.Substring(0, 8).ToCharArray());

            //MemoryStream oMemory = new MemoryStream(bytValor, 0, bytValor.Length);
            //CryptoStream oCStream = new CryptoStream(oMemory, oDes.CreateDecryptor(bytKey, IV), CryptoStreamMode.Read);

            //StreamReader oSr = new StreamReader(oCStream);

            //return oSr.ReadToEnd();

            return strTexto;
        }
    }
}
